﻿#include "Trader.h"
#include "SymbolDetail.h"
#include "Tools.h"
#include "TradeOrderIO.h"

Trader::Trader(MetricsDataGather *pGather, QObject *parent)
	: QObject(parent), m_dataGather(pGather)
{
}

Trader::~Trader()
{
}

void Trader::InitAllSymbols(time_t pCircleEnd)
{
	std::list<std::string> symbolList;
	SymbolPackage::GetTheLastMainSymbolList(symbolList, Tools::GetCurrentTimeStamp());

	std::set<std::string> symbolsSet;
	for (std::list<std::string>::iterator it = symbolList.begin(); it != symbolList.end(); ++it)
	{
		symbolsSet.insert(*it);
	}
	for (std::map<std::string, TD::Order>::const_iterator it = m_TradeHandle->GetAllOrderDetail().begin(); it != m_TradeHandle->GetAllOrderDetail().end(); ++it)
	{
		symbolsSet.insert(it->first);
	}
	for (std::set<std::string>::iterator it = symbolsSet.begin(); it != symbolsSet.end(); ++it)
	{
		InitSymbol(*it, pCircleEnd);
	}
}

void Trader::InitSymbol(const std::string &pSymbol, time_t pCircleEnd)
{
	StrategyBase *pStrategy = m_strategyManager.GetStrategy(pSymbol, m_TradeHandle);
	if (pStrategy != nullptr)
	{
		pStrategy->ClearInitData(pSymbol);
		pStrategy->SetStrategyDataGather(m_dataGather);
		pStrategy->Init(pSymbol, pCircleEnd);
	}

	//initTradeData(pSymbol);
	//std::shared_ptr<PrepareStrategyBase> supportStrategy = m_strategyManager.GetSupportStrategy(pSymbol);
	//if (supportStrategy.get() == nullptr)
	//{
	//	return;
	//}
	//std::map<std::string, FrameTradeOrder>::iterator orderIt = m_tradeOrder.find(pSymbol);
	//supportStrategy->Init(pSymbol, pCircleEnd, orderIt != m_tradeOrder.end());
	//if (!supportStrategy->GetSupportData().CloserSupportValues.empty())
	//{
	//	m_prepareData[pSymbol] = supportStrategy->GetSupportData();
	//}
}


void Trader::OnDealMarket(const std::string &pSymbol, time_t lastNewKTime, const MData::PriceItem &priceItem)
{
	StrategyBase *pStrategy = m_strategyManager.GetStrategy(pSymbol, m_TradeHandle);
	if (pStrategy != nullptr)
	{
		pStrategy->OnMarketData(pSymbol, lastNewKTime, priceItem);
	}
}

void Trader::ClearInitData(const std::string &pSymbol)
{
	StrategyBase *pStrategy = m_strategyManager.GetStrategy(pSymbol, m_TradeHandle);
	if (pStrategy != nullptr)
	{
		pStrategy->ClearInitData(pSymbol);
	}
}
